﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class KartuPiutang : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument reportDocument;

        public KartuPiutang()
        {
            DA = new DataAccess<object>();
            reportDocument = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public void LoadReport(DateTime tglStart)
        {
            StringBuilder sql = new StringBuilder();
            var Source = DA.GetDataListDataSet(String.Format("Exec usp_GetReportPiutang '{0}'",tglStart.ToString("yyyy-MM-dd")));
            string reportPath = Server.MapPath("~/Report/AgingPiutang.rpt");
            reportDocument.Load(reportPath);
            reportDocument.SetDataSource(Source.Tables[0]);
            SetupParameter(tglStart);
            CrystalReportViewer1.ReportSource = reportDocument;
            CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }

        private void SetupParameter(DateTime tglStart)
        {
            var param = new ParameterDiscreteValue();
            param.Value = tglStart.ToString("dd-MM-yyyy");
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            reportDocument.DataDefinition.ParameterFields["tgl"].ApplyCurrentValues(paramValue);
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTgl1.Text))
                LoadReport(DateTime.Parse(txtTgl1.Text, new CultureInfo("ID-id")));
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            reportDocument.Clone();
            reportDocument.Dispose();
        }

     
    }
}